package com.ss.ttm.net;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Pattern;

public class AVResolver {
    private static final int MAX_CACHED = 128;
    private static final String TAG = "ttmj";
    private static final Hashtable<String, a> mCacheHosts = new Hashtable();
    public static int HOST_MAX_CACHE_TIME = 600000;
    private String mError;
    private a mHostInfo;
    private String mHostName;
    private String[] mIPStr;
    private boolean mRet;
    private Thread mThread;

    public AVResolver() {
        this.mRet = false;
        this.mThread = null;
    }

    public static final boolean isIP(String arg3) {
        boolean v0 = false;
        if (arg3.length() >= 7 && arg3.length() <= 15) {
            v0 = Pattern.compile("([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}").matcher(((CharSequence) arg3)).find();
        } else if (arg3.charAt(0) == 91 && arg3.charAt(arg3.length() - 1) == 93) {
            v0 = true;
        }

        return v0;
    }

    static synchronized void putHostInfo(String arg10, a arg11) {
        Object v1;
        Object v2_1;
        Object v0 = null;
        Class v6 = AVResolver.class;
        synchronized (v6) {
            long v2 = System.currentTimeMillis();
            if (AVResolver.mCacheHosts.size() > 128) {
                Iterator<Map.Entry<String, a>> v7 = AVResolver.mCacheHosts.entrySet().iterator();
                long v4 = v2;
                v2_1 = v0;
                while (true) {
                    label_13:
                    if (v7.hasNext()) {
                        v0 = v7.next().getValue();
                        v1 = v7.next().getKey();
                        if (v0 != null && ((a) v0).b < v4) {
                            v4 = ((a) v0).b;
                            v2_1 = v0;
                            v0 = v1;
                            continue;
                        }
                        v0 = v1;
                        continue;
                    } else {
                        v1 = v0;
                        v0 = v2_1;
                        break;
                    }
                }
            } else {
                v1 = v0;
            }

            label_29:
            if (v0 != null && v1 != null) {
                AVResolver.mCacheHosts.remove(v1);
            }

            AVResolver.mCacheHosts.put(arg10, arg11);
        }
    }

    public void freeAddress() {
        if (this.mThread != null) {
            try {
                this.mThread.interrupt();
            } catch (Exception v0) {
                v0.printStackTrace();
            }
        }
    }

    public String getAddress() {
        String v0 = !this.mRet || this.mIPStr == null || this.mIPStr[0] == null ? "parser host name: " + this.mHostName + " error.err msg:" + this.mError : this.mIPStr[0];
        return v0;
    }

    public void getAddressInfo(String arg7) {
        this.mHostName = arg7;
        if (arg7 == null || arg7.length() <= 0 || ("".equals(arg7))) {
            this.mRet = true;
        } else if (AVResolver.isIP(this.mHostName)) {
            this.mIPStr = new String[1];
            this.mIPStr[0] = this.mHostName;
            this.mRet = true;
        } else {
            this.mHostInfo = AVResolver.mCacheHosts.get(arg7);
            if (this.mHostInfo != null) {
                if (this.mHostInfo.a != null && System.currentTimeMillis() - this.mHostInfo.b < (((long) AVResolver.HOST_MAX_CACHE_TIME))) {
                    this.mIPStr = new String[1];
                    this.mIPStr[0] = this.mHostInfo.a;
                    this.mRet = true;
                    return;
                }

                AVResolver.mCacheHosts.remove(arg7);
                this.mHostInfo = null;
            }

            try {
                this.mThread = new Thread(new b(this, this.mHostName));
                this.mThread.start();
            } catch (Exception v0) {
                this.mRet = true;
                this.mError = v0.getMessage();
            }
        }
    }

    public int isSuccess() {
        int v0 = 0;
        if (this.mRet) {
            if (this.mIPStr != null && this.mIPStr[0] != null) {
                return 1;
            }

            v0 = -1;
        }

        return v0;
    }

    final class a {
        public String a;
        public long b;

        a() {

        }
    }

    final class b implements Runnable {
        private AVResolver a;
        private String b;

        public b(AVResolver arg2, String arg3) {

            this.a = null;
            this.a = arg2;
            this.b = arg3;
        }

        public final void run() {
            InetAddress v0 = null;
            try {
                v0 = InetAddress.getByName(this.b);
            } catch (UnknownHostException v1) {
                v1.printStackTrace();
                this.a.mError = v1.getMessage();
            }

            this.a.mRet = true;
            if (v0 != null) {
                String v1_1 = v0.getHostAddress();
                this.a.mIPStr = new String[1];
                String v0_1 = (v0 instanceof Inet6Address) ? String.format("[%s]", v1_1) : v1_1;
                this.a.mIPStr[0] = v0_1;
                a v1_2 = new a();
                v1_2.b = System.currentTimeMillis();
                v1_2.a = v0_1;
                AVResolver.putHostInfo(this.b, v1_2);
                v1_2.b = System.currentTimeMillis();
            }
        }
    }
}

